﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnalyseCrmMana;

namespace AnalyseCRM.InteractiveData
{
    public partial class RolesManagementAjax : System.Web.UI.Page
    {
        private string _paramValue = "", _paramType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["paramvalue"] != null)
            {
                _paramValue = Server.UrlDecode(Request["paramvalue"].ToString());
            }
            if (Request["paramtype"] != null)
            {
                _paramType = Request["paramtype"].ToString();
            }
            Response.Write(buildHtml(_paramValue, _paramType));
        }

        public StringBuilder buildHtml(string prparamValue, string prparamType)
        {
            StringBuilder returnValue = null;
            RolesMana rolemana = null;
            switch (prparamType)
            {
                #region InsertUsers
                case "InsertRoles":
                    returnValue = new StringBuilder();
                    string[] accpara = prparamValue.Split('|');
                    rolemana = new RolesMana();
                    rolemana.roleno = accpara[0];
                    rolemana.rolename = accpara[1];
                    rolemana.roledesc = accpara[2];
                    if (rolemana.AddNewRole(rolemana.roleno,rolemana.rolename,rolemana.roledesc) > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("ERROR");
                    }
                    break;
                #endregion
                case "getRolesList":
                    returnValue = new StringBuilder();
                    rolemana = new RolesMana();
                    IList<RolesMana> rolelist = rolemana.GetAllRoleList();
                    int egcount = rolemana.GetAllRoleListCount();
                    if (rolelist != null && rolelist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(rolelist));
                        returnValue.Append("$" + egcount);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                case "deleteRole":
                    returnValue = new StringBuilder();
                    rolemana = new RolesMana();
                    if (rolemana.DeleteRole(prparamValue) > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("ERROR");
                    }
                    break;
                #region 获取所有角色不分页
                case "getAllRolesList":
                    returnValue = new StringBuilder();
                    rolemana = new RolesMana();
                    IList<RolesMana> allrolelist = rolemana.GetAllRoleList();
                    if (allrolelist != null && allrolelist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(allrolelist));
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region 获取所有权限
                case "getAllauthority":
                    returnValue = new StringBuilder();
                    AuthorityMana authmana = new AuthorityMana();
                    IList<AuthorityMana> authlist = authmana.getAllauthoritys();
                    //int egcount = authmana.getAllauthoritysCount();
                    if (authlist != null && authlist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(authlist));
                        //returnValue.Append("$" + egcount);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region 根据选择的角色获取权限
                case "selectauthoritybyroleno":
                    returnValue = new StringBuilder();
                    SelectAuthority selauth = new SelectAuthority();
                    IList<SelectAuthority> selauthlist = selauth.getAllauthoritysByrole(prparamValue);
                    //int egcount = authmana.getAllauthoritysCount();
                    if (selauthlist != null && selauthlist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(selauthlist));
                        //returnValue.Append("$" + egcount);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region 绑定角色与权限
                case "bindroleauthority":
                    returnValue = new StringBuilder();
                    rolemana = new RolesMana();
                    string[] arrayparam = prparamValue.Split('|');
                    int recount = 0;
                    if (arrayparam[2] == "D")
                    {
                        recount = rolemana.DelRoleAndAuthority(arrayparam[0], Convert.ToInt32(arrayparam[1]));
                    }
                    else if (arrayparam[2] == "A")
                    {
                        recount = rolemana.RelRoleAndAuthority(arrayparam[0], Convert.ToInt32(arrayparam[1]));
                    }
                    if (recount > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                #endregion
                default:
                    break;
            }
            return returnValue;
        }  
    }
}